(function () {
    //当前ajax的数量
    var currentAjaxCount = 0;
    var setTime;

    function hideLoadingClassBack() {//AJAX 请求成功时执行函数
        currentAjaxCount--;
        if (currentAjaxCount != 0) return;
        clearTimeout(setTime);
        setTimeout(function () {
            $('#loadingPageDimmer').removeClass('active');
            document.body.style.height = "";
            document.body.style.minHeight = "";
            if (!!$.fn.getNiceScroll && !zlin.htmlScrollHide) {
                $("html").getNiceScroll().show();
            }
            $(window).trigger('ajaxLoad');
        }, 200);

    };
    $.ajaxSetup({
        cache: false,
        //发送请求前触发
        beforeSend: function () {
            if (!window.zlin.isLoadAnimation)return;
            currentAjaxCount++;
            if (currentAjaxCount != 1) return;
            setTime = setTimeout(function () {
                if (!!$.fn.getNiceScroll && !zlin.htmlScrollHide) {
                    $("html").getNiceScroll().hide();
                }
                $('#loadingPageDimmer .spinner').css("top", document.body.scrollTop + ($(window).height() / 2));
                $('#loadingPageDimmer').height($(document).height()).addClass('active');
                $("body").css({"min-height": $(window).height()});
            }, 500);
        },
        //完成请求后触发。即在success或error触发后触发
        complete: function (xhr, s) {
            if (window.zlin.isLoadAnimation) hideLoadingClassBack();
            if (xhr.status != '401') {
                return;
            }
            function back() {
                location.href = '/login';
            }

            $.alert({
                title: "错误",
                content: "登录超时",
                theme: "error",
                confirm: back,
                cancel: back
            });
        }
    });
})();
jQuery.extend({
    param: (function (param) {

        function encode(key, value) {
            return encodeURIComponent(key) + '=' + encodeURIComponent(value);//escape();
        }

        function add$Array(a) {
            var s = [];
            $.each(a, function () {
                s.push(encode(this.name, this.value));
            });
            return s;
        }

        function addObject(j, a) {
            var s = [];
            $.each(a, function (key, value) {
                if (!$.isFunction(value)) {
                    if ($.isPlainObject(value)) {
                        s.pushAll(addObject(j + key + '.', value));
                    }
                    else if ($.isArray(a) || a.jquery) {
                        s.pushAll(addArray(j + key + '.', value));
                    }
                    else {
                        s.push(encode(j + key, value));
                    }
                }
            });
            return s;
        }

        function addArray(j, a) {
            var s = [];
            $.each(a, function (_index) {
                if ($.isPlainObject(this)) {
                    s.pushAll(addObject(j + '[' + _index + '].', this));
                } else {
                    s.push(encode(j, this));
                }
            });
            return s;
        }

        return function (a) {
            var s = [];
            if ($.isArray(a) || a.jquery)
                s.pushAll(add$Array(a));
            else
                for (var j in a)
                    if ($.isArray(a[j]))
                        s.pushAll(addArray(j, a[j]));
                    else if ($.isPlainObject(a[j]))
                        s.pushAll(addObject(j + '.', a[j]));
                    else
                        s.push(encode(j, $.isFunction(a[j]) ? a[j]() : a[j]));
            return s.join("&");
        };
    })(jQuery.fn.param)
});
